#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <glob.h>
#include <sys/types.h>
#include <sys/wait.h>

#define DELIMS 	" \t\n"

struct cmd_st
{
	glob_t globres;
};


static void prompt(void);
static void parse(char * line, struct cmd_st * pres);

int main(int argc, char * argv[])
{
	pid_t pid;
	char * linebuf = NULL;
	size_t linebuf_size = 0;
	struct cmd_st cmd;

	while(1)
	{
		prompt();
		if(getline(&linebuf, &linebuf_size, stdin) < 0)
			break;
		parse(linebuf, &cmd);
		if(0)
		{
			//do somthing
		}
		else
		{
			pid = fork();
			if(pid < 0)
			{
				perror("fork");
				exit(1);
			}
			if(pid == 0)
			{
				execvp(cmd.globres.gl_pathv[0], cmd.globres.gl_pathv);
				perror("execvp");
				exit(1);
			}
			wait(NULL);
		}
	}
	exit(0);
}
static void prompt(void)
{
	printf("mysh-0.1$ ");
	fflush(stdout);
}
static void parse(char * line, struct cmd_st * pres)
{
	char * tok;
	int i = 0;

	while(1)
	{
		tok = strsep(&line, DELIMS);
		if(tok == NULL)
			break;
		if(tok [0] == '\0')
			continue;
		glob(tok, GLOB_NOCHECK | GLOB_APPEND * i, NULL, &pres->globres);
		i = 1;
	}
}
